﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Web.ViewModels.SearchQuery;

namespace AutoPartsSearch.Web.App_Start.AutoMapperConverters
{
    public class SearchQueryTypeConverter : ITypeConverter<SearchQueryAdd, List<SearchQuery>>
    {
        public List<SearchQuery> Convert(ResolutionContext context)
        {
            List<SearchQuery> result = new List<SearchQuery>();
            var searchQueryAdd = context.SourceValue as SearchQueryAdd;
            for (int i = 0; i < searchQueryAdd.Parts.Count; i++)
            {
                var searchQuery = new SearchQuery
                                      {
                                          PartCatalogNumber = searchQueryAdd.Parts[i].PartCatalogNumber,
                                          PartDescription = searchQueryAdd.Parts[i].PartDescription,
                                          PartGroup = new Dictionary(searchQueryAdd.Parts[i].PartGroup.Value),
                                          PartName = searchQueryAdd.Parts[i].PartName,
                                          PartState = new Dictionary(searchQueryAdd.Parts[i].PartState.Value),
                                          PartType = new Dictionary(searchQueryAdd.Parts[i].PartType.Value),
                                          CarBrand = new Dictionary(searchQueryAdd.CarBrand.Value),
                                          CarEngine = searchQueryAdd.CarEngine,
                                          CarModel = searchQueryAdd.CarModel,
                                          CarModification = searchQueryAdd.CarModification,
                                          CarCase =         searchQueryAdd.CarCase.HasValue ? new Dictionary(searchQueryAdd.CarCase.Value) : null,
                                          CarDrive = searchQueryAdd.CarDrive.HasValue ? new Dictionary(searchQueryAdd.CarDrive.Value) : null,
                                          CarTransmission = searchQueryAdd.CarTransmission.HasValue ? new Dictionary(searchQueryAdd.CarTransmission.Value) : null,
                                          CarType = searchQueryAdd.CarType.HasValue ? new Dictionary(searchQueryAdd.CarType.Value) : null,
                                          CarFuelType = searchQueryAdd.CarFuelType.HasValue ? new Dictionary(searchQueryAdd.CarFuelType.Value) : null,
                                          CarVin = searchQueryAdd.CarVin,
                                          CarYear = searchQueryAdd.CarYear.Value,
                                          City = new Dictionary(searchQueryAdd.City),
                                          Comment = searchQueryAdd.Comment,
                                          Email = searchQueryAdd.Email,
                                          Name = searchQueryAdd.Name,
                                          Phone = searchQueryAdd.Phone,
                                          Phone2 = searchQueryAdd.Phone2
                                      };
                result.Add(searchQuery);
            }
            return result;
        }
    }
}